package com.luo.storage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.luo.storage.model.domain.PageQueryParams;
import com.luo.storage.model.domain.Result;
import com.luo.storage.model.domain.User;
import com.luo.storage.model.domain.UserRole;
import com.luo.storage.service.UserRoleService;
import com.luo.storage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;

//    @PostMapping("/userRole")
//    public Result addUserRole(@RequestBody List<UserRole> userRoleList){
//        userRoleList.stream()
//                        .map()
//        userRoleService.saveBatch(ids);
//        return Result.success();
//    }

    /**
     * 分页查询
     * @param pageQueryParams
     * @return
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageQueryParams pageQueryParams){
        IPage<User> userPage =userService.userPage(pageQueryParams);
        return Result.success(userPage);
    }

    @PostMapping
    public Result add(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody User user){
        userService.updateById(user);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        userService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id){
        userService.getById(id);
        return Result.success();
    }
}
